package IO.序列化流;

import java.io.*;

public class 序列化和反序列化 {
    /**
     * 序列化读取 必须要用到字节流和 Object字节流：
     * ObjectOutputStream  ObjectInputStream
     *
     * @param args
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        User user = new User(1, "马凡格", 23);
        File file = new File("D:/IOFile/test/person.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        FileOutputStream fom = new FileOutputStream(file);
        //序列化写入流
        ObjectOutputStream oot = new ObjectOutputStream(fom);
        oot.writeObject(user);
        oot.close();

        FileInputStream  fis = new FileInputStream(file);
        //反序列化读入流
        ObjectInputStream ois = new ObjectInputStream(fis);

        User readUser = (User)ois.readObject();
        int id = readUser.getId();
        int age = readUser.getAge();
        String name= readUser.getName();
        System.out.println("id:"+ id +"\t姓名:" + name  + "\tage:" + age);
        ois.close();
    }
}
